﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace GSLMS.DAL.Oracle
{
	/// <summary>
	/// 数据访问类:APP_PROCOMINFO
	/// </summary>
	public partial class APP_PROCOMINFO : BaseDAL<GSLMS.Entity.TB_APP_PROCOMINFO>
	{
		private readonly GSLMS.Core.Loger log = new Core.Loger();
		public APP_PROCOMINFO()
		{}
		#region  Method
		/// <summary>
		///  增加一条数据
		/// </summary>
		public override bool Add(GSLMS.Entity.TB_APP_PROCOMINFO model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_REGINFOID", model.REGINFOID),
								new OracleParameter("P_PROID", model.PROID),
								new OracleParameter("P_PROGID", model.PROGID),
								new OracleParameter("P_PROGTYPE", model.PROGTYPE),
								new OracleParameter("P_PROGNO", model.PROGNO),
								new OracleParameter("P_PROGCAT", model.PROGCAT),
								new OracleParameter("P_AMT", model.AMT),
								new OracleParameter("P_RATE", model.RATE),
								new OracleParameter("P_MANAGERATE", model.MANAGERATE),
								new OracleParameter("P_GENERALRATE", model.GENERALRATE),
								new OracleParameter("P_LOANTIME", model.LOANTIME),
								new OracleParameter("P_LOANPHASE", model.LOANPHASE),
								new OracleParameter("P_LOANPHASETYPE", model.LOANPHASETYPE),
								new OracleParameter("P_ADVAMT", model.ADVAMT),
								new OracleParameter("P_AUDITREMARK", model.AUDITREMARK),
								new OracleParameter("P_REMARK", model.REMARK),
								new OracleParameter("P_CONFIRMBY", model.CONFIRMBY),
								new OracleParameter("P_CONFIRMTIME", model.CONFIRMTIME)};
  
			for (int i = 0; i < 19; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                int num=OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_procominfo_add", parms);
                if (num == 1)
                    flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

        /// <summary>
        /// 确认立项申请的方案信息
        /// </summary>
        /// <param name="model">传入PROID,PROGID,CONFIRMBY,REMARK</param>
        /// <returns></returns>
        public bool ConfrimSolu(GSLMS.Entity.TB_APP_PROCOMINFO model)
        {
            bool flag = false;
            
            OracleParameter[] parms = new OracleParameter[]{
								new OracleParameter("p_proid", model.PROID),
								new OracleParameter("p_progid", model.PROGID),
								new OracleParameter("p_confirmby", model.CONFIRMBY),
								new OracleParameter("P_REMARK", model.REMARK),
								new OracleParameter("returnvalue",OracleType.Int32)};

            for (int i = 0; i <=3; i++)
            {
                parms[i].Direction = ParameterDirection.Input;
            }

            parms[4].Direction = ParameterDirection.Output;
            try
            {
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_procominfo_sel", parms);
                if (parms[4].Value != DBNull.Value && OracleHelper.ToInt(parms[4].Value.ToString())==1)
                    flag = true;
            }
            catch (Exception ex)
            {
                log.ExceptionHand(ex);
            }
           

            

            return flag;
        }

        /// <summary>
        /// 立项申请确认方案时退回
        /// </summary>
        /// <param name="proID"></param>
        /// <param name="backBy"></param>
        /// <param name="backReason"></param>
        /// <returns></returns>
        public bool BackTo(long proID,string backBy,string backReason)
        {
            bool flag = false;

            OracleParameter[] parms = new OracleParameter[]{
								new OracleParameter("p_proid",proID),
								new OracleParameter("p_backby", backBy),
								new OracleParameter("p_backreason", backReason),
								new OracleParameter("returnvalue",OracleType.Int32)};

            for (int i = 0; i <= 2; i++)
            {
                parms[i].Direction = ParameterDirection.Input;
            }

            parms[3].Direction = ParameterDirection.Output;
            try
            {
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_procominfo_backto", parms);
                if (parms[3].Value != DBNull.Value && OracleHelper.ToInt(parms[3].Value.ToString()) == 1)
                    flag = true;
            }
            catch (Exception ex)
            {
                log.ExceptionHand(ex);
            }
           


            return flag;
        }



		/// <summary>
		///  更新一条数据,不再使用
		/// </summary>
		public override bool Update(GSLMS.Entity.TB_APP_PROCOMINFO model)
		{
			bool flag = false;
            //OracleParameter[] parms =new OracleParameter[]{
            //                    new OracleParameter("P_ID", model.ID),
            //                    new OracleParameter("P_REGINFOID", model.REGINFOID),
            //                    new OracleParameter("P_PROID", model.PROID),
            //                    new OracleParameter("P_PROGID", model.PROGID),
            //                    new OracleParameter("P_PROGTYPE", model.PROGTYPE),
            //                    new OracleParameter("P_PROGNO", model.PROGNO),
            //                    new OracleParameter("P_PROGCAT", model.PROGCAT),
            //                    new OracleParameter("P_AMT", model.AMT),
            //                    new OracleParameter("P_RATE", model.RATE),
            //                    new OracleParameter("P_MANAGERATE", model.MANAGERATE),
            //                    new OracleParameter("P_GENERALRATE", model.GENERALRATE),
            //                    new OracleParameter("P_LOANTIME", model.LOANTIME),
            //                    new OracleParameter("P_LOANPHASE", model.LOANPHASE),
            //                    new OracleParameter("P_LOANPHASETYPE", model.LOANPHASETYPE),
            //                    new OracleParameter("P_ADVAMT", model.ADVAMT),
            //                    new OracleParameter("P_AUDITREMARK", model.AUDITREMARK),
            //                    new OracleParameter("P_REMARK", model.REMARK),
            //                    new OracleParameter("P_CONFIRMBY", model.CONFIRMBY),
            //                    new OracleParameter("P_CONFIRMTIME", model.CONFIRMTIME)};
  
            //for (int i = 0; i < 19; i++)
            //{
            //    parms[i].Direction = ParameterDirection.Input;
            //}

            //try
            //{
            //   int num=OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_procominfo_add", parms);
            //   if (num == 1)
            //       flag = true;
            //}
            //catch (Exception ex)
            //{
            //    log.ExceptionHand(ex);
            //}
			return flag;
		}

		/// <summary>
		///  删除一条数据,不再使用
		/// </summary>
		public override bool Delete(long ID)
		{
			bool flag = false;
            //OracleParameter[] parms =new OracleParameter[]{
            //                    new OracleParameter("P_ID", ID)};
  
            //    parms[0].Direction = ParameterDirection.Input;

            //try
            //{
            //    OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_app_procominfo_delete", parms);
            //    flag = true;
            //}
            //catch (Exception ex)
            //{
            //    log.ExceptionHand(ex);
            //}
			return flag;
		}

		/// <summary>
		///DataRowToModel 
		/// </summary>
		private GSLMS.Entity.TB_APP_PROCOMINFO DataRowToModel(DataRow dr)
		{
			if (dr != null && dr.ItemArray.Length > 0)
			{
				GSLMS.Entity.TB_APP_PROCOMINFO model = new GSLMS.Entity.TB_APP_PROCOMINFO();
				if(dr["ID"]!=null && dr["ID"].ToString()!="")
				{
					model.ID=OracleHelper.ToLong(dr["ID"].ToString());
				}
				if(dr["REGINFOID"]!=null && dr["REGINFOID"].ToString()!="")
				{
					model.REGINFOID=OracleHelper.ToLong(dr["REGINFOID"].ToString());
				}
				if(dr["PROID"]!=null && dr["PROID"].ToString()!="")
				{
					model.PROID=OracleHelper.ToLong(dr["PROID"].ToString());
				}
				if(dr["PROGID"]!=null && dr["PROGID"].ToString()!="")
				{
					model.PROGID=OracleHelper.ToLong(dr["PROGID"].ToString());
				}
				if(dr["PROGTYPE"]!=null && dr["PROGTYPE"].ToString()!="")
				{
                    model.PROGTYPE = OracleHelper.ToInt(dr["PROGTYPE"].ToString());
				}
				if(dr["PROGNO"]!=null && dr["PROGNO"].ToString()!="")
				{
                    model.PROGNO = OracleHelper.ToInt(dr["PROGNO"].ToString());
				}
				if(dr["PROGCAT"]!=null && dr["PROGCAT"].ToString()!="")
				{
					model.PROGCAT=dr["PROGCAT"].ToString();
				}
				if(dr["AMT"]!=null && dr["AMT"].ToString()!="")
				{
					model.AMT=OracleHelper.ToDecimal(dr["AMT"].ToString());
				}
				if(dr["RATE"]!=null && dr["RATE"].ToString()!="")
				{
					model.RATE=OracleHelper.ToDecimal(dr["RATE"].ToString());
				}
				if(dr["MANAGERATE"]!=null && dr["MANAGERATE"].ToString()!="")
				{
					model.MANAGERATE=OracleHelper.ToDecimal(dr["MANAGERATE"].ToString());
				}
				if(dr["GENERALRATE"]!=null && dr["GENERALRATE"].ToString()!="")
				{
					model.GENERALRATE=OracleHelper.ToDecimal(dr["GENERALRATE"].ToString());
				}
				if(dr["LOANTIME"]!=null && dr["LOANTIME"].ToString()!="")
				{
                    model.LOANTIME = OracleHelper.ToInt(dr["LOANTIME"].ToString());
				}
				if(dr["LOANPHASE"]!=null && dr["LOANPHASE"].ToString()!="")
				{
                    model.LOANPHASE = OracleHelper.ToInt(dr["LOANPHASE"].ToString());
				}
				if(dr["LOANPHASETYPE"]!=null && dr["LOANPHASETYPE"].ToString()!="")
				{
                    model.LOANPHASETYPE = OracleHelper.ToInt(dr["LOANPHASETYPE"].ToString());
				}
				if(dr["ADVAMT"]!=null && dr["ADVAMT"].ToString()!="")
				{
					model.ADVAMT=OracleHelper.ToDecimal(dr["ADVAMT"].ToString());
				}
				if(dr["AUDITREMARK"]!=null && dr["AUDITREMARK"].ToString()!="")
				{
					model.AUDITREMARK=dr["AUDITREMARK"].ToString();
				}
				if(dr["REMARK"]!=null && dr["REMARK"].ToString()!="")
				{
					model.REMARK=dr["REMARK"].ToString();
				}
				if(dr["CONFIRMBY"]!=null && dr["CONFIRMBY"].ToString()!="")
				{
					model.CONFIRMBY=dr["CONFIRMBY"].ToString();
				}
				if(dr["CONFIRMTIME"]!=null && dr["CONFIRMTIME"].ToString()!="")
				{
					model.CONFIRMTIME=DateTime.Parse(dr["CONFIRMTIME"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public override GSLMS.Entity.TB_APP_PROCOMINFO GetByID(long ID)
		{
			GSLMS.Entity.TB_APP_PROCOMINFO sModel = new GSLMS.Entity.TB_APP_PROCOMINFO();
			string sql = "";
			sql = "select * from TB_APP_PROCOMINFO where id=" + ID.ToString();
			DataSet ds = OracleHelper.ExecuteDataset(sql);
			if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				sModel = DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
			return null;
			}
			return sModel;
		}

        /// <summary>
        /// 验证是否已经确认方案信息
        /// </summary>
        /// <param name="proID">立项申请记录ID</param>
        /// <returns></returns>
        public bool IsSolued(long proID)
        {
            bool flag = false;
            string sql = "";
            sql = "select count(1) from TB_APP_PROCOMINFO where proid="+proID.ToString();

            var obj = OracleHelper.ExecuteScalar(OracleHelper.connectionString,CommandType.Text,sql);

            if (obj != null && OracleHelper.ToInt(obj.ToString()) > 0)
                flag = true;

            return flag;
        }

		/// <summary>
		/// 分页提取数据
		/// </summary>
		public override List<GSLMS.Entity.TB_APP_PROCOMINFO> GetList(GSLMS.Entity.TB_APP_PROCOMINFO searchParam)
		{
			var list=new List<GSLMS.Entity.TB_APP_PROCOMINFO>();
            //DataTable dt = null;
            //string tblName = "TB_APP_PROCOMINFO";
            //string sqlWhere="";
            //string strSort="";
            //int pageSize=20;
            //int pageIndex=1;
            //string strFields="*";
            //int totalCount = 0;
            //int countFlag = 1;
            //dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex,countFlag, sqlWhere, out totalCount);
            //if (dt != null && dt.Rows.Count > 0)
            //{
            //    for (int i = 0; i < dt.Rows.Count; i++)
            //    {
            //        GSLMS.Entity.TB_APP_PROCOMINFO model = new GSLMS.Entity.TB_APP_PROCOMINFO();
            //        model = DataRowToModel(dt.Rows[i]);
            //        list.Add(model);
            //    }
            //}
            //else
            //{
            //    return null;
            //}
			return list;
		}


        /// <summary>
        /// 通过立项申请记录ID提取确认方案信息
        /// </summary>
        /// <param name="proID"></param>
        /// <returns></returns>
        public List<GSLMS.Entity.TB_APP_PROCOMINFO> GetAllList(long proID)
        {
            var list = new List<GSLMS.Entity.TB_APP_PROCOMINFO>();
            DataSet ds = null;
            DataTable dt = null;


            OracleParameter[] parms = new OracleParameter[] { 
              new OracleParameter("p_proid",proID),
              new OracleParameter("p_cur",OracleType.Cursor)
            };

            parms[0].Direction = ParameterDirection.Input;
            parms[1].Direction = ParameterDirection.Output;

             ds = OracleHelper.ExecuteDataset(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_procominfo_get", parms);
            
            if (ds != null && ds.Tables.Count > 0)
                dt = ds.Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    GSLMS.Entity.TB_APP_PROCOMINFO model = new GSLMS.Entity.TB_APP_PROCOMINFO();
                    model = DataRowToModel(dt.Rows[i]);
                    list.Add(model);
                }
            }
            else
            {
                return null;
            }

            return list;
        }

        /// <summary>
        /// 通过立项申请记录ID提取已经确认方案信息
        /// </summary>
        /// <param name="proID"></param>
        /// <returns></returns>
        public GSLMS.Entity.TB_APP_PROCOMINFO GetConfirmProComInfo(long proID)
        {
            var list = new List<GSLMS.Entity.TB_APP_PROCOMINFO>();
            DataSet ds = null;
            DataTable dt = null;
            string sql = "";
            GSLMS.Entity.TB_APP_PROCOMINFO model = new GSLMS.Entity.TB_APP_PROCOMINFO();

            sql = "select * from TB_APP_PROCOMINFO where  proid="+proID.ToString()+" and confirmby is not null";

           

            ds = OracleHelper.ExecuteDataset(sql);

            if (ds != null && ds.Tables.Count > 0)
                dt = ds.Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {
                model = DataRowToModel(dt.Rows[0]);
            }
            else
            {
                return null;
            }

            return model;
        }



		#endregion  Method
	}
}

